//
//  YoujiViewController.m
//  Travel
//
//  Created by lanou3g on 15/4/11.
//  Copyright (c) 2015年 &#21556;&#21697;&#27901;. All rights reserved.
//

#import "YoujiViewController.h"
#import <AFNetworking.h>
#import "Travelnote.h"
#import "TravelTableViewCell.h"
#import "DetailViewController.h"
#import "UserViewController.h"
@interface YoujiViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)NSMutableArray *arr1;
@property (nonatomic,retain)UITableView *tableView1;
@end

@implementation YoujiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView1AndTableView2];
    [self requestForTableView1];
}
- (void)createTableView1AndTableView2
{
    self.title = [NSString stringWithFormat:@"%@游记",self.numberNameYouji];
    self.tableView1 = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView1.delegate = self;
    self.tableView1.dataSource = self;
    //  [self.tableview1 registerNib:[UINib nibWithNibName:@"TravelCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"travelCell"];
    self.tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView1];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *str = @"hehe";
        TravelTableViewCell *cell = [self.tableView1 dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[TravelTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        Travelnote *notes = [self.arr1 objectAtIndex:indexPath.row];
        cell.notes = notes;
    cell.userButton.tag = indexPath.row;
    cell.targetAction = self;
    
        return cell;
    
}
- (void)clickUser:(UIButton *)button
{
    Travelnote *usernote = [self.arr1 objectAtIndex:button.tag];
    UserViewController *user = [[UserViewController alloc]init];
    
    NSNumber *nu = [usernote.user objectForKey:@"id"];
    NSString *str = [NSString stringWithFormat:@"%@",nu];
    NSString *title = [usernote.user objectForKey:@"name"];
    user.hidesBottomBarWhenPushed = YES;
    user.userID = str;
    user.gaTitle = title;
    NSLog(@"按钮Tag值: %ld , 用户ID : %@",button.tag,str);
    [self.navigationController pushViewController:user animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr1.count;
}

- (void)requestForTableView1
{
    self.arr1 = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/destinations/trips/%@.json?month=0&page=1",self.numberYouji];
    NSLog(@"!!!!!!!!!!!!%@",string);
    //NSMutableArray *arrs = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:string parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *array = [NSMutableArray arrayWithArray:responseObject];
        for (NSMutableDictionary *dic in array) {
            Travelnote *note = [[Travelnote alloc]init];
            [note setValuesForKeysWithDictionary:dic];
            [self.arr1 addObject:note];
        }
        NSLog(@"222222222222222 %ld",self.arr1.count);
        [self.tableView1 reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求出错");
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 160;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        Travelnote *note = [self.arr1 objectAtIndex:indexPath.row];
        DetailViewController *detal = [[DetailViewController alloc]init];
        detal.hidesBottomBarWhenPushed = YES;
        detal.IDNumber = note.ID;
        [self.navigationController pushViewController:detal animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
